{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 设计类"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-09-26T06:27:52.747984600Z",
     "start_time": "2024-09-26T06:27:52.731042Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三 18 男 100\n"
     ]
    }
   ],
   "source": [
    "#设计一个类（类比生活中设计一张表格）\n",
    "class student:\n",
    "    name = None\n",
    "    age = None\n",
    "    genger = None\n",
    "    score = None\n",
    "#创建一个对象（类比生活中打印一张表格）\n",
    "s1 = student()\n",
    "#对对象属性进行赋值（类比生活中填写一张表格）\n",
    "s1.name = \"张三\"\n",
    "s1.age = 18\n",
    "s1.genger = \"男\"\n",
    "s1.score = 100\n",
    "#输出信息（查看表格）\n",
    "print(s1.name,s1.age,s1.genger,s1.score)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 类的成员方法\n",
    "面向对象编程的思想：先构建一个类，再根据类创建对象，对象去干活"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    name = None\n",
    "    def sayhi(self,msg):\n",
    "        print(f\"hello,i'm:{self.name},{msg}\")#必须要有self这个参数，负责方法无法访问类的成员变量\n",
    "\n",
    "s1 = student()\n",
    "s1.name = \"张三\"\n",
    "s1.sayhi(\"nice to meet you\")#传递参数self相当于不存在，不需要传这个参数\n",
    "s2 = student()\n",
    "s2.name = \"李四\"\n",
    "s2.sayhi(\"nice to meet you too\")\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 构造方法（类似c++构造函数）\n",
    "在构造类时会自动执行构造方法，并且构建类对象的传参会传递给构造方法，从而给成员变量赋值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    name = None\n",
    "    age = None\n",
    "    score = None\n",
    "\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "\n",
    "\n",
    "s1 = student(\"张三\", 18, 100)\n",
    "print(s1.name, s1.age, s1.score)\n",
    "s2 = student(\"李四\", 19, 99)\n",
    "print(s2.name, s2.age, s2.score)\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 魔术方法\n",
    "__str__"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "    def __str__(self):#通过str魔术方法控制他的返回值\n",
    "        return f\"student类对象,姓名：{self.name} ,年龄：{self.age} ,成绩：{self.score}\"\n",
    "\n",
    "stu = student(\"张三\", 18, 100)\n",
    "print(stu)\n",
    "print(str(stu))\n",
    "\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## __lt__表示两个类的属性大小关系（>or<）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "\n",
    "    def __lt__(self, other):\n",
    "        return self.score < other.score\n",
    "\n",
    "stu1 = student(\"张三\", 18, 100)\n",
    "stu2 = student(\"李四\", 19, 99)\n",
    "\n",
    "print(stu1 > stu2)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## __le__ 魔术方法（加入等于）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "class student:\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "\n",
    "    def __le__(self, other):\n",
    "        return self.score <= other.score\n",
    "\n",
    "stu1 = student(\"张三\", 18, 100)\n",
    "stu2 = student(\"李四\", 19, 99)\n",
    "\n",
    "print(stu1 >= stu2)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## __eq__ 判断等于方法"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "class student:\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "\n",
    "    def __eq__(self, other):\n",
    "        return self.score == other.score\n",
    "\n",
    "stu1 = student(\"张三\", 18, 100)\n",
    "stu2 = student(\"李四\", 19, 99)\n",
    "\n",
    "print(stu1 == stu2)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-09-26T07:04:45.785573800Z",
     "start_time": "2024-09-26T07:04:45.770621Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
